rm(list=ls(all=TRUE))
library(metafor)
library(clubSandwich)
library(dplyr)

setwd("~/Dropbox/Apps/ShareLaTeX/Minimal Effects/replication/data")

data <- read.csv("MPSA Persuasion MetaAnalysis - Issue Campaign.csv", stringsAsFactors = FALSE)

# Recoding fun
data <- rename(data, CACE = CACE..in.pp., SE = SE..in.pp.)

data$variance <- data$SE^2
data$precision <- 1/data$variance

data$Experiment <- factor(data$Experiment...for.clustering)

# Competitiveness
data$Competitive <- ifelse(data$Competitive == 'Yes', 1, 0)

#In-Person Treatment vs. Not
data$Personal <- ifelse(data$Treatment.Mode %in% c("Candidate Canvass", "Canvass",
                                                   "Phone", "Phone and canvass"), 1, 0)

# Type of Measurement
data$Precinct <- ifelse(data$Measurement.mode == 'Precinct', 1, 0)


# Overall
meta <- rma.mv(yi = CACE, V = variance, random = list(~1 | Experiment), data = data)
coef_test(meta, vcov = "CR2")

# Overall plots
pdf('../figures/issues.pdf', width = 10, height = 5.2)
forest(meta, slab = paste0(data$Citation, ' - ', data$Treatment.Mode), cex = .5, xlab = 'Estimated Treatment Effect (CACE) in Percentage Points and 95% Confidence Interval \n Subset: Ballot Measure Elections',
       alim=c(-20,20), xlim=c(-40,32))
dev.off()

